package spec.ecore;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.impl.EPackageImpl;

public class EcoreClassDiagram {

	private HashMap<String, EcorePackage> pack_list;

	public EcoreClassDiagram(ArrayList<EPackageImpl> list_pack) {
		pack_list = new HashMap<String, EcorePackage>();
		travelPackage("", list_pack);
	}

	private void travelPackage(String packName, ArrayList<EPackageImpl> list_pack) {
		for (EPackageImpl next : list_pack) {
			if (next.getESubpackages().size() > 0) {
				ArrayList<EPackageImpl> sub_list = new ArrayList<EPackageImpl>();
				for (EPackage pack : next.getESubpackages()) {
					if (pack instanceof EPackageImpl) {
						sub_list.add((EPackageImpl) pack);
					}
				}
				String name = packName.equals("") ? next.getName() : packName + "." + next.getName();
				travelPackage(name, sub_list);
			}

			if (next.getEClassifiers().size() > 0) {
				String name = packName.equals("") ? next.getName() : packName + "." + next.getName();
				EcorePackage ePack = new EcorePackage(name, next);
				pack_list.put(name, ePack);
			}
		}
	}

	public ArrayList<String[]> searchVariable(String className, String methodName) {
		if (pack_list.size() > 0) {
			for (String key : pack_list.keySet()) {
				return pack_list.get(key).searchVariable(className, methodName);
			}
		}

		return null;
	}

	public List<String[]> searchParamVariable(String className, String methodName) {
		if (pack_list.size() > 0) {
			for (String key : pack_list.keySet()) {
				return pack_list.get(key).searchParamVariable(className, methodName);
			}
		}

		return null;
	}
	
	public Collection<EcorePackage> getAllPackage(){
		return pack_list.values();
	}

}
